package com.ruoyi.web.controller.logistics;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.annotation.AfterEsTrigger;
import com.ruoyi.logistics.domain.DTO.QcLogisticsTrackingEsDTO;
import com.ruoyi.logistics.domain.Vo.QcLogisticsTrackingVo;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.logistics.domain.QcLogisticsTracking;
import com.ruoyi.logistics.service.IQcLogisticsTrackingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物流追踪Controller
 *
 * @author ruoyi
 * @date 2025-05-24
 */
@RestController
@RequestMapping("/logistics/logisticsTracking")
public class QcLogisticsTrackingController extends BaseController {
    @Autowired
    private IQcLogisticsTrackingService qcLogisticsTrackingService;

    /**
     * 查询物流追踪列表
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:list')")
    @GetMapping("/list")
    public TableDataInfo list(QcLogisticsTracking qcLogisticsTracking) {
        startPage();
        List<QcLogisticsTracking> list = qcLogisticsTrackingService.selectQcLogisticsTrackingList(qcLogisticsTracking);
        return getDataTable(list);
    }

    /**
     * 查询物流追踪列表
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:list')")
    @GetMapping("/eslist")
    public TableDataInfo list(QcLogisticsTrackingVo qcLogisticsTracking) throws IOException {
        SearchHits<QcLogisticsTrackingEsDTO> list = qcLogisticsTrackingService.selectQcLogisticsTrackingEsList(qcLogisticsTracking);
        return getEsDataTable(list);
    }

    /**
     * 查询物流追踪列表
     */
    @GetMapping("/getScrollToPage")
    public AjaxResult getScrollToPage(QcLogisticsTrackingVo qcLogisticsTracking) throws IOException {

        return AjaxResult.success(qcLogisticsTrackingService.getScrollToPage(qcLogisticsTracking));
    }


    /**
     * 导出物流追踪列表
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:export')")
    @Log(title = "物流追踪", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcLogisticsTracking qcLogisticsTracking) {
        List<QcLogisticsTracking> list = qcLogisticsTrackingService.selectQcLogisticsTrackingList(qcLogisticsTracking);
        ExcelUtil<QcLogisticsTracking> util = new ExcelUtil<QcLogisticsTracking>(QcLogisticsTracking.class);
        util.exportExcel(response, list, "物流追踪数据");
    }

    /**
     * 获取物流追踪详细信息
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(qcLogisticsTrackingService.selectQcLogisticsTrackingById(id));
    }

    /**
     * 新增物流追踪
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:add')")
    @Log(title = "物流追踪", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcLogisticsTracking qcLogisticsTracking) {
        return toAjax(qcLogisticsTrackingService.insertQcLogisticsTracking(qcLogisticsTracking));
    }

    /**
     * 修改物流追踪
     */

    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:edit')")
    @Log(title = "物流追踪", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcLogisticsTracking qcLogisticsTracking) {
        return toAjax(qcLogisticsTrackingService.updateQcLogisticsTracking(qcLogisticsTracking));
    }

    /**
     * 删除物流追踪
     */
    @PreAuthorize("@ss.hasPermi('logistics:logisticsTracking:remove')")
    @Log(title = "物流追踪", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qcLogisticsTrackingService.deleteQcLogisticsTrackingByIds(ids));
    }

    /**
     * 导入数据
     *
     * @param file
     * @return
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) {

        try {
            InputStream inputStream = file.getInputStream();
            qcLogisticsTrackingService.importQcLogisticsTracking(inputStream, qcLogisticsTrackingService);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("导入失败");
        }
        return AjaxResult.success("导入成功,请稍后列表数据");
    }

    /**
     * 导入超期数量模板导出
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {

        ExcelUtil<QcLogisticsTracking> excelUtil = new ExcelUtil<>(QcLogisticsTracking.class);
        excelUtil.importTemplateExcel(response, "导入物流跟踪表");
    }


}
